﻿namespace Chain.BLL
{
    using Chain.IDAL;
    using Chain.Model;
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Runtime.InteropServices;

    public class Staff
    {
        private readonly Chain.IDAL.Staff dal = new Chain.IDAL.Staff();

        public int Add(Chain.Model.Staff model)
        {
            if (this.dal.Exists(model.StaffID, model.StaffNumber))
            {
                return -1;
            }
            return this.dal.Add(model);
        }

        public List<Chain.Model.Staff> DataTableToList(DataTable dt)
        {
            List<Chain.Model.Staff> list = new List<Chain.Model.Staff>();
            int count = dt.Rows.Count;
            if (count > 0)
            {
                for (int i = 0; i < count; i++)
                {
                    Chain.Model.Staff item = new Chain.Model.Staff();
                    if ((dt.Rows[i]["StaffID"] != null) && (dt.Rows[i]["StaffID"].ToString() != ""))
                    {
                        item.StaffID = int.Parse(dt.Rows[i]["StaffID"].ToString());
                    }
                    if ((dt.Rows[i]["StaffNumber"] != null) && (dt.Rows[i]["StaffNumber"].ToString() != ""))
                    {
                        item.StaffNumber = dt.Rows[i]["StaffNumber"].ToString();
                    }
                    if ((dt.Rows[i]["StaffName"] != null) && (dt.Rows[i]["StaffName"].ToString() != ""))
                    {
                        item.StaffName = dt.Rows[i]["StaffName"].ToString();
                    }
                    if ((dt.Rows[i]["StaffSex"] != null) && (dt.Rows[i]["StaffSex"].ToString() != ""))
                    {
                        if ((dt.Rows[i]["StaffSex"].ToString() == "1") || (dt.Rows[i]["StaffSex"].ToString().ToLower() == "true"))
                        {
                            item.StaffSex = true;
                        }
                        else
                        {
                            item.StaffSex = false;
                        }
                    }
                    if ((dt.Rows[i]["StaffMobile"] != null) && (dt.Rows[i]["StaffMobile"].ToString() != ""))
                    {
                        item.StaffMobile = dt.Rows[i]["StaffMobile"].ToString();
                    }
                    if ((dt.Rows[i]["StaffAddress"] != null) && (dt.Rows[i]["StaffAddress"].ToString() != ""))
                    {
                        item.StaffAddress = dt.Rows[i]["StaffAddress"].ToString();
                    }
                    if ((dt.Rows[i]["StaffClassID"] != null) && (dt.Rows[i]["StaffClassID"].ToString() != ""))
                    {
                        item.StaffClassID = int.Parse(dt.Rows[i]["StaffClassID"].ToString());
                    }
                    if ((dt.Rows[i]["StaffRemark"] != null) && (dt.Rows[i]["StaffRemark"].ToString() != ""))
                    {
                        item.StaffRemark = dt.Rows[i]["StaffRemark"].ToString();
                    }
                    list.Add(item);
                }
            }
            return list;
        }

        public bool Delete(int StaffID)
        {
            return this.dal.Delete(StaffID);
        }

        public bool DeleteList(string StaffIDlist)
        {
            return this.dal.DeleteList(StaffIDlist);
        }

        public bool Exists(int StaffID)
        {
            return this.dal.Exists(StaffID);
        }

        public bool Exists(int intStaffID, string strStaffNumber)
        {
            return this.dal.Exists(intStaffID, strStaffNumber);
        }

        public DataSet GetAllList()
        {
            return this.GetList("");
        }

        public DataSet GetList(string strWhere)
        {
            return this.dal.GetList(strWhere);
        }

        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            return this.dal.GetList(Top, strWhere, filedOrder);
        }

        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            return this.dal.GetListByPage(strWhere, orderby, startIndex, endIndex);
        }

        public DataSet GetListSP(int PageSize, int PageIndex, out int resCount, string strTime, params string[] strWhere)
        {
            return this.dal.GetListSP(PageSize, PageIndex, out resCount, strTime, strWhere);
        }

        public int GetMaxId()
        {
            return this.dal.GetMaxId();
        }

        public Chain.Model.Staff GetModel(int StaffID)
        {
            return this.dal.GetModel(StaffID);
        }

        public List<Chain.Model.Staff> GetModelList(string strWhere)
        {
            DataSet list = this.dal.GetList(strWhere);
            return this.DataTableToList(list.Tables[0]);
        }

        public int GetRecordCount(string strWhere)
        {
            return this.dal.GetRecordCount(strWhere);
        }

        public int Update(Chain.Model.Staff model)
        {
            if (this.dal.Exists(model.StaffID, model.StaffNumber))
            {
                return -1;
            }
            return this.dal.Update(model);
        }
    }
}

